package client.ui;

import client.business.ContactManager;
import server.db.Contact;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddContactDialog extends JDialog {
    private JTextField nameField;
    private JTextField phoneField;
    private JTextField addressField;
    private JButton addButton;
    private JButton cancelButton;
    private ContactManager contactManager;

    public AddContactDialog(Frame owner) {
        super(owner, "添加联系人", true);
        contactManager = new ContactManager();

        nameField = new JTextField(20);
        phoneField = new JTextField(20);
        addressField = new JTextField(20);
        addButton = new JButton("添加");
        cancelButton = new JButton("取消");

        setLayout(new GridLayout(4, 2));
        add(new JLabel("姓名:"));
        add(nameField);
        add(new JLabel("电话号码:"));
        add(phoneField);
        add(new JLabel("地址:"));
        add(addressField);
        add(addButton);
        add(cancelButton);

        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText();
                String phone = phoneField.getText();
                String address = addressField.getText();
                contactManager.addContact(name, phone, address);
                dispose();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        pack();
        setLocationRelativeTo(owner);
    }
}
